﻿using AntdUI;
using BST250301.Models;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using HslCommunication;
using HslCommunication.Core;
using HslCommunication.Profinet;
using HslCommunication.Profinet.AllenBradley;
using HslCommunication.Profinet.Omron;
using MathNet.Numerics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Client;
using Mysqlx.Crud;
using NPOI.SS.Formula.PTG;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BST250301.Views
{
    public partial class ConfigPLC : UserControl
    {
        #region Head
        HslCommunication.Profinet.Omron.OmronConnectedCipNet omronEtherNetCIP { get; set; }
        HslCommunication.Profinet.Siemens.SiemensS7Net siemensS7Net { get; set; }
        HslCommunication.Profinet.Siemens.SiemensMPI siemensMPI { get; set; }
        HslCommunication.Profinet.Siemens.SiemensPPIOverTcp siemensPPIOverTcp { get; set; }
        HslCommunication.Profinet.Omron.OmronCipNet omronCipNet { get; set; }
        HslCommunication.Profinet.Omron.OmronFinsNet omronFinsNet { get; set; }
        HslCommunication.Profinet.Melsec.MelsecMcAsciiNet melsecMcAsciiNet { get; set; }
        HslCommunication.Profinet.Melsec.MelsecCipNet melsecCipNet { get; set; }
        HslCommunication.Profinet.Melsec.MelsecMcUdp melsecMcUdp { get; set; }
        #endregion
        private AppConfig _config;
        public ConfigPLC()
        {
            InitializeComponent();
            BindEventHandler();
            InitData();
            if (!MainWindow.userCurrent.IsSuperAdmin)
            { this.Enabled = false; }
        }

        private void BindEventHandler()
        {

        }
        private async Task InitData()
        {
            this.inputPLCIP.Text = MainWindow.configapp.PLCIP;
            this.inputPLCPort.Text = MainWindow.configapp.PLCPort.ToString();
            this.selectPLCnet.Text = MainWindow.configapp.PLCType;
        }

        private void inputNumberDelay_ValueChanged(object sender, DecimalEventArgs e)
        {

        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(inputPLCPort.Text, out int port))
            {
                MessageBox.Show("错误的端口号");
                return;
            }
            try
            {
                PLCConfig plc = new PLCConfig();
                plc.RemotePLCtype = selectPLCnet.Text;
                plc.RemotePLCport = port;
                plc.RemotePLCaddress = inputPLCIP.Text;
                plc.ConnectTimeOut = 3000;
                plc.ReceiveTimeOut = 100;
                bool res = plc.ConnectPLC(true);
                if (res)
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                }
                else
                {
                    MessageBox.Show(StringResources.Language.ConnectedFailed);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                MainWindow.configapp.PLCIP = inputPLCIP.Text;
                int.TryParse(inputPLCPort.Text, out int port);
                MainWindow.configapp.PLCPort = port;
                MainWindow.configapp.PLCType = selectPLCnet.Text;
                MainWindow.configapp.Save();
            }
            catch (Exception ex)
            {

            }

        }

        private async void buttonTestNVH_Click(object sender, EventArgs e)
        {
            int.TryParse(inputNVHport.Text, out int port);
            var client = new TCP(inputNVHip.Text, port);
            await client.ConnectAsync();
            await client.SendAsync("\\00\\00\\00\\06$1#");
            MessageBox.Show("NVH连接测试成功");
            await Task.Delay(3000);
            client.Disconnect();
        }

        private void buttonSaveNVH_Click(object sender, EventArgs e)
        {
            try
            {
                MainWindow.configapp.NVHIP = inputNVHip.Text;
                if (int.TryParse(inputNVHport.Text, out int port))
                    MainWindow.configapp.NVHPort = port;
                MainWindow.configapp.Save();
            }
            catch (Exception ex)
            {

            }
        }

        private void buttonCANSave_Click(object sender, EventArgs e)
        {

        }

        private void buttonRDPara_Click(object sender, EventArgs e)
        {

            var rp = new RecordData();
            var form = new RecordPara(rp);// { Size = new Size(500, 350) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {

            }
        }
    }
}
